package com.nari.ysphd.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RelativeLayout;

public class RatRelativeLayout extends RelativeLayout {

    private static final String TAG = RatRelativeLayout.class.getSimpleName();

    private int widthRatio = 16;
    private int heightRatio = 9;

    public RatRelativeLayout(Context context) {
        this(context, null);
    }

    public RatRelativeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RatRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int screenWidth = MeasureSpec.getSize(heightMeasureSpec);
        int screenHeight = MeasureSpec.getSize(widthMeasureSpec);
//        if ((screenWidth * 1.0 / screenHeight) > (widthRatio * 1.0 / heightRatio)) {
            int width = MeasureSpec.getSize(heightMeasureSpec) * widthRatio / heightRatio;
            int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
            Log.d(TAG, "onMeasure: 宽屏 w*h==>" + width + "*" + screenHeight);
            super.onMeasure(widthSpec, heightMeasureSpec);
//        } else {
//            int height = MeasureSpec.getSize(widthMeasureSpec) * heightRatio / widthRatio;
//            int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
//            Log.d(TAG, "onMeasure: 窄屏 w*h==>" + screenWidth + "*" + height);
//            super.onMeasure(widthMeasureSpec, heightSpec);
//        }
    }
}
